// Copyright 2008 Thiago H. de Paula Figueiredo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package br.com.arsmachina.module.service.internal;

import org.apache.tapestry5.ioc.Invocation;
import org.apache.tapestry5.ioc.MethodAdvice;

/**
 * {@link MethodAdvice} that changes a {@link Class} parameter into its superclass if the 
 * parameter is a Hibernate Javassist proxy class.
 * 
 * @author Thiago H. de Paula Figueiredo.
 */
final public class JavassistMethodAdvice implements MethodAdvice {

	/**
	 * Checks if the given {@link Invocation} has only one parameter of type {@link Class}.
	 * If yes, it solves the Javassist proxy class problem by overriding the parameter before
	 * the invocation is proceeded.
	 */
	@SuppressWarnings("unchecked")
	public void advise(Invocation invocation) {
		
		if (invocation.getParameterCount() == 1 && invocation.getParameterType(0).equals(Class.class)) {
		
			Class clasz = (Class) invocation.getParameter(0);
			
			if (clasz.getName().contains("$$_javassist_")) {
				invocation.override(0, clasz.getSuperclass());
			}
			
		}
		
		invocation.proceed();
		
	}
	
}